package com.zhangsun.system.component;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.zhangsun.system.entity.DepartmentEntity;
import com.zhangsun.system.entity.ResourceEntity;
import com.zhangsun.system.entity.RoleEntity;
import com.zhangsun.system.enums.ResourceTypeEnum;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class SystemComponent {

    /**
     * 获取角色
     *
     * @param roleEntities 角色
     * @return 角色
     */
    public Set<String> getRoles(List<RoleEntity> roleEntities) {
        Set<String> roles = new HashSet<>();
        if (CollUtil.isNotEmpty(roleEntities)) {
            roleEntities.forEach(r -> roles.add(r.getRole()));
        }
        return roles;
    }

    /**
     * 获取权限字符串
     *
     * @param resourceEntities 资源
     * @return 权限
     */
    public Set<String> getPermissions(List<ResourceEntity> resourceEntities) {
        Set<String> permissions = new HashSet<>();
        if (CollUtil.isNotEmpty(resourceEntities)) {
            resourceEntities.forEach(r -> permissions.add(r.getPermission()));
        }
        return permissions;
    }

    /**
     * 整理成菜单
     *
     * @param resourceList 资源
     * @param justMenu     仅显示菜单
     * @return 菜单
     */
    public List<ResourceEntity> getResourceTree(List<ResourceEntity> resourceList, boolean justMenu) {
        List<ResourceEntity> menus = new ArrayList<>();
        if (CollUtil.isNotEmpty(resourceList)) {
            for (ResourceEntity resource : resourceList) {
                if (StrUtil.isBlank(resource.getParentId())) {
                    if (!justMenu || ResourceTypeEnum.MENU.equals(resource.getType())) {
                        menus.add(resource);
                    }
                }
            }
            if (CollUtil.isNotEmpty(menus)) {
                for (ResourceEntity menu : menus) {
                    List<ResourceEntity> children = getResourceChildren(menu.getId(), resourceList, justMenu);
                    menu.setChildren(children);
                }
            }
        }
        return menus;
    }

    /**
     * 获取子菜单
     *
     * @param parentId     上级id
     * @param resourceList 资源
     * @param justMenu     仅显示菜单
     * @return 子菜单
     */
    private List<ResourceEntity> getResourceChildren(String parentId, List<ResourceEntity> resourceList, boolean justMenu) {
        List<ResourceEntity> children = new ArrayList<>();
        for (ResourceEntity resource : resourceList) {
            if (parentId.equals(resource.getParentId()) && (!justMenu || ResourceTypeEnum.MENU.equals(resource.getType()))) {
                children.add(resource);
            }
        }
        if (CollUtil.isNotEmpty(children)) {
            for (ResourceEntity child : children) {
                List<ResourceEntity> childList = getResourceChildren(child.getId(), resourceList, justMenu);
                child.setChildren(childList);
            }
        }
        return children;
    }

    /**
     * 整理成部门树
     *
     * @param departmentList 部门
     * @return 部门树
     */
    public List<DepartmentEntity> getDepartmentTree(List<DepartmentEntity> departmentList) {
        List<DepartmentEntity> departments = new ArrayList<>();
        if (CollUtil.isNotEmpty(departmentList)) {
            for (DepartmentEntity department : departmentList) {
                if (StrUtil.isBlank(department.getParentId())) {
                    departments.add(department);
                }
            }
            if (CollUtil.isNotEmpty(departments)) {
                for (DepartmentEntity department : departments) {
                    List<DepartmentEntity> children = getDepartmentChildren(department.getId(), departmentList);
                    department.setChildren(children);
                }
            }
        }
        return departments;
    }

    /**
     * 获取子部门
     *
     * @param parentId       上级id
     * @param departmentList 部门
     * @return 子部门
     */
    private List<DepartmentEntity> getDepartmentChildren(String parentId, List<DepartmentEntity> departmentList) {
        List<DepartmentEntity> children = new ArrayList<>();
        for (DepartmentEntity department : departmentList) {
            if (parentId.equals(department.getParentId())) {
                children.add(department);
            }
        }
        if (CollUtil.isNotEmpty(children)) {
            for (DepartmentEntity child : children) {
                List<DepartmentEntity> childList = getDepartmentChildren(child.getId(), departmentList);
                child.setChildren(childList);
            }
        }
        return children;
    }

}
